package scau.erps.view.base;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import scau.erps.app.Erps;
import scau.erps.entity.Line;
import scau.erps.entity.Store;
import scau.erps.service.LineService;
import scau.erps.service.ProductService;
import scau.erps.service.ResearchService;
import scau.erps.service.impl.LineServiceImpl;
import scau.erps.service.impl.ProductServiceImpl;
import scau.erps.service.impl.ResearchServiceImpl;


/**
 * 开始下一批生产对话框
 * @author outsider
 *
 */
public class NewProductDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
    private JComboBox<String> avliableLineComboBox;
    private LineService lineService=Erps.getIocInstance().get(LineServiceImpl.class);
    private ProductService productService=Erps.getIocInstance().get(ProductServiceImpl.class);
    private int team_Id=Erps.getCurrentTeam().getId();
    private DecisionPanel superPanel;
    private boolean crystal=false,ruby=false,sapphire=false;
    private boolean show=true;
  	public NewProductDialog(DecisionPanel superPanel) {
		setTitle("开始下一批生产");
        this.superPanel=superPanel;
		this.setSize(300,225);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("请选择需要可投产的生产线");
			lblNewLabel.setBounds(30, 10, 240, 35);
			contentPanel.add(lblNewLabel);
		}
		{
			avliableLineComboBox = new JComboBox<String>();
			avliableLineComboBox.setBounds(30, 50, 200, 35);			
		}		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定投产");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						okButtonAction();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("退出");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						if (avliableLineComboBox.getItemCount()>=1){
							int reg=JOptionPane.showConfirmDialog(null,"闲置生产线放弃投产？","确认",JOptionPane.YES_NO_OPTION);
							if (reg==JOptionPane.YES_OPTION) dispose();
						}
						else dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		//判断产品是否研发完成
		
		ResearchService researchService = Erps.getIocInstance().get(
				ResearchServiceImpl.class);
		if (researchService.findResearchs(team_Id, "Crystal") == 4) {
			crystal=true;
		}
		if (researchService.findResearchs(team_Id, "Ruby") == 6) {
			ruby=true;
		}
		if (researchService.findResearchs(team_Id, "Sapphire") == 8) {
			sapphire=true;
		}
		//获取当前可生产生产线
		List<Line> lineList=lineService.getLineByTeamId(team_Id);

		for (Line line: lineList){
			String status=line.getStatus();
			int lineNo=line.getLineNo();
			String type=line.getType();
			String product=line.getProduct();
			if (type.equals("柔性线")==false){
            if (product.equals("Crystal") && crystal==false) continue;
            if (product.equals("Ruby") && ruby==false) continue;
            if (product.equals("Sapphire") && sapphire==false) continue; 
			}
			if (status.equals("空闲")||status.equals("全新")){
	             String message=lineNo+"号线 "+type+" "+product;
	             if (type.equals("柔性线")) message=lineNo+"号线 "+type+" Any";
	             avliableLineComboBox.addItem(message);
			}
		}	
		if (avliableLineComboBox.getItemCount()==0){
			JOptionPane.showMessageDialog(this, "当前无可用生产线","提示",JOptionPane.INFORMATION_MESSAGE);
			show=false;
		}
		contentPanel.add(avliableLineComboBox);
        if (show) setVisible(true);
	}
	private void okButtonAction(){
		int num=1,flag;
		Boolean bo = false;
		int lineNo=Integer.parseInt(avliableLineComboBox.getSelectedItem().toString().substring(0, 1));
		Line line=lineService.getLineByLineNo(team_Id, lineNo);
		if (line.getType().equals("柔性线")){                    //若当前生产线为柔性线
			List<String> listProduct=new ArrayList<String>();     //获取该柔性线当前可生产的产品列表
			listProduct.add("Beryl");
			if (crystal==true) listProduct.add("Crystal");
			if (ruby==true) listProduct.add("Ruby");
			if (sapphire==true) listProduct.add("Sapphire");
			String productType=(String) JOptionPane.showInputDialog(null,"请选择该柔性线生产产品种类:\n", "选择产品", JOptionPane.PLAIN_MESSAGE, null, listProduct.toArray(), null);
			if (productType==null) return;
			flag=productService.preNewProduct(team_Id,productType,lineNo,num,line);
		}
		else
			flag=productService.preNewProduct(team_Id,line.getProduct(),lineNo,num,line);
		if (flag>=0){
			String message=lineNo+"号线已开始生产";
			JOptionPane.showMessageDialog(null, message, "提示",JOptionPane.INFORMATION_MESSAGE);
			if (bo==false) avliableLineComboBox.removeItem(avliableLineComboBox.getSelectedItem());
			superPanel.getDashBoardPanel().setFactoryData();
			superPanel.getDashBoardPanel().setDashBoardData(18);
			if (avliableLineComboBox.getItemCount()==0) dispose();
			return;			
		}
		String message = null;
		if (flag<=-1&&flag>=-4) message="原材料M"+(-flag)+"储量不足";
		if (flag==-5) message="Beryl"+"储量不足";
        if (flag==-6) message="现金不足，请进行融资操作";
        if (flag==-7) message="该产品研发未完成，无法进行生产";
		JOptionPane.showMessageDialog(null, message, "提示",JOptionPane.INFORMATION_MESSAGE);
		return;	
	}  
}
